import tkinter as tk
from tkinter import END
sum = ''
is_clean = 1
window = tk.Tk()
window.title("计算器")
window.geometry("325x430")
window.resizable(0,0)
entry = tk.Entry(width=18,font=("黑体",23),justify='right')
entry.place(x = 15,y = 10)
entry.config(state='normal')
entry.insert(tk.END, "0")
entry.config(state='readonly')
def num(str):
    global dian,is_clean
    if  str == '00'  or str == '0' or str == ")" or str == "%" or str == "取余":
        if entry.get() == "0":
            return
    if str == ".":
        if entry.get() == "0":
            is_clean = 0
    if is_clean == 1:
        entry.config(state='normal')
        entry.delete(0,tk.END)
        entry.config(state='readonly')
        is_clean = 0
    entry.config(state='normal')
    entry.insert(tk.END, str)
    entry.config(state='readonly')
def C():
    global dian
    entry.config(state='normal')
    entry.delete(0, tk.END)
    entry.insert(tk.END, "0")
    entry.config(state='readonly')
    dian = 1;
def D():
    global dian,is_clean;
    entry.config(state='normal')
    try:
        if entry.get()[len(entry.get())-1] == ".":
            dian = 1;
    except IndexError:
        pass
    entry.delete(len(entry.get())-1)
    if entry.get() == "":
        entry.insert(tk.END, "0")
        is_clean = 1
    entry.config(state='readonly')
def dy():
    global dian,is_clean
    entry.config(state='normal')
            
    s1 = entry.get()
    s = ''
    for i in s1:
        if i == "×":
            s += "*"
        elif i == "÷":
            s += "/"
        elif i == "%":
            s += "/100"
        elif i == "取":
            s += "%"
        elif i == "余":
            pass
        else:
            s += i
    entry.delete(0, tk.END)
    try:
        entry.insert(END, str(eval(s)))
    except:
        entry.insert(END,"错误")
    entry.config(state='readonly')
    dian = 1;
    is_clean = 1
color_font = "#000000"
color = "#ffffff"
tk.Button(window,bd = 2,text='OFF',command=window.destroy,font=("黑体",20)).place(x = 25,y = 55)
tk.Button(window,bd = 2,text=' C ',command=C,font=("黑体",20)).place(x = 95,y = 55)
tk.Button(window,bd = 2,text='<--',command=D,font=("黑体",20)).place(x = 165,y = 55)


tk.Button(window,bd = 2,text=' 1 ',command=lambda:num("1"),font=("黑体",20)).place(x = 25,y = 115)
tk.Button(window,bd = 2,text=' 2 ',command=lambda:num("2"),font=("黑体",20)).place(x = 95,y = 115)
tk.Button(window,bd = 2,text=' 3 ',command=lambda:num("3"),font=("黑体",20)).place(x = 165,y =115)

tk.Button(window,bd = 2,text=' 4 ',command=lambda:num("4"),font=("黑体",20)).place(x = 25,y =175)
tk.Button(window,bd = 2,text=' 5 ',command=lambda:num("5"),font=("黑体",20)).place(x = 95,y =175)
tk.Button(window,bd = 2,text=' 6 ',command=lambda:num("6"),font=("黑体",20)).place(x = 165,y =175)

tk.Button(window,bd = 2,text=' 7 ',command=lambda:num("7"),font=("黑体",20)).place(x = 25,y =235)
tk.Button(window,bd = 2,text=' 8 ',command=lambda:num("8"),font=("黑体",20)).place(x = 95,y =235)
tk.Button(window,bd = 2,text=' 9 ',command=lambda:num("9"),font=("黑体",20)).place(x = 165,y =235)

tk.Button(window,bd = 2,text=' 0 ',command=lambda:num("0"),font=("黑体",20)).place(x = 25,y =295)
tk.Button(window,bd = 2,text=' . ',command=lambda:num("."),font=("黑体",20)).place(x = 165,y =295)
tk.Button(window,bd = 2,text='00 ',command=lambda:num("00"),font=("黑体",20)).place(x = 95,y =295)

tk.Button(window,bd = 2,text=' + ',command=lambda:num("+"),font=("黑体",20)).place(x = 235,y =55)
tk.Button(window,bd = 2,text=' - ',command=lambda:num("-"),font=("黑体",20)).place(x = 235,y =115)
tk.Button(window,bd = 2,text='× ',command=lambda:num("×"),font=("黑体",20)).place(x = 235,y =175)
tk.Button(window,bd = 2,text='÷ ',command=lambda:num("÷"),font=("黑体",20)).place(x = 235,y =235)
tk.Button(window,bd = 2,text=' % ',command=lambda:num("%"),font=("黑体",20)).place(x = 235,y =295)
tk.Button(window,bd = 2,text=' = ',command=dy,font=("黑体",20)).place(x = 235,y =355)

tk.Button(window,bd = 2,text=' (',command=lambda:num("("),font=("黑体",20)).place(x = 25,y =355)
tk.Button(window,bd = 2,text=') ',command=lambda:num(")"),font=("黑体",20)).place(x = 95,y =355)
tk.Button(window,bd = 2,text='取余',command=lambda:num("取余"),font=("黑体",20)).place(x = 155,y =355)
window.mainloop()
